import {
	changePassword,
	coolOrderList,
	shareOrderList,
	liveOutOrder,
	extensionOrder,
	extensionCheck,
	callClearRomm,
	callClock,
	liveOutConfirm,
	openDoor
} from '@/api/api.js'
export default{
	data() {
		return {
			modalName:null,
			orderList:[],
			pwd:null,
			newPwd:null,
			current:0,
			show:false,
			params:{},
			isClock:false,
			day:1,
		};
	},
	onLoad(){
		
	},
	onShow(){
		if(uni.getStorageSync('Authorization')){
			this.getOrder();
			this.current=0;
		}
	},
	methods:{
		openDoor(){
			if(this.orderList.length<=0){
				uni.showToast({
					icon:"none",
					title:"未入住"
				})
				return;
			}
			if(this.orderList[this.current].orderState<4){
				uni.showToast({
					icon:"none",
					title:"未接单，不能远程开门"
				})
				return;
			}
			uni.showModal({
			    title: '提示',
			    content: '是否远程开门',
			    success: (res)=>{
			        if (res.confirm) {
						this.open();
			        }
			    }
			});
		},
		open(){
			uni.showLoading({
				mask:true,
				title:'正在开锁'
			})
			openDoor({
				orderInfoId:this.orderList[this.current].id,
			}).then(res=>{
				uni.hideLoading()
				if(res.code==200){
					uni.showToast({
						icon:"success",
						title:"开门成功"
					})
				}
			}).catch(err=>{
				uni.hideLoading()
				uni.showModal({
				    title: '错误',
				    content: '开门失败是否继续',
					success:(rs)=> {
						if(rs.confirm){
							this.open()
						}
					}
				})
			})
		},
		bluetoothOpenDoor(){
			if(this.orderList.length<=0){
				uni.showToast({
					icon:"none",
					title:"未入住"
				})
				return;
			}
			if(this.orderList[this.current].shareFlag==1){
				uni.showToast({
					icon:"none",
					title:"共享订单不能使用蓝牙开门"
				})
				return;
			}
			uni.showModal({
			    title: '提示',
			    content: '是否蓝牙开门',
			    success: (res)=>{
			        if (res.confirm) {
			            console.log('开门');
			        }
			    }
			});
		},
		savePrice(type){
			liveOutConfirm({
				orderInfoId:this.orderList[this.current].id,
				type
			}).then(res=>{
				if(res.code==200){
					this.hideModal()
					if(type){
						uni.showToast({
							icon:"none",
							title:"确认成功，稍后押金会退还至微信"
						})
					}else{
						uni.showToast({
							icon:"none",
							title:"反馈成功，稍后我们会有客服致电，请注意接听"
						})
					}
				}
			})
		},
		confirm(e){
			if(this.isClock){
				uni.showModal({
				    title: '提示',
				    content: '是否确认',
				    success: (res)=>{
				        if (res.confirm) {
				            callClock({
				            	orderInfoId:this.orderList[this.current].id,
								time:e.hour+":"+e.minute
				            }).then(res=>{
				            	if(res.code==200){
				            		uni.showToast({
				            			icon:"success",
				            			title:"操作成功"
				            		})
				            	}
				            })
				        }
				    }
				});
			}
		},
		clearRoom(){
			if(this.orderList.length<=0){
				uni.showToast({
					icon:"none",
					title:"未入住"
				})
				return;
			}
			if(this.orderList[this.current].orderState<4){
				uni.showToast({
					icon:"none",
					title:"未接单，不能呼叫打扫"
				})
				return;
			}
			uni.showModal({
			    title: '提示',
			    content: '是否呼叫打扫',
			    success: (res)=>{
			        if (res.confirm) {
			            callClearRomm({
			            	orderInfoId:this.orderList[this.current].id
			            }).then(res=>{
			            	if(res.code==200){
			            		uni.showToast({
			            			icon:"success",
			            			title:"呼叫成功"
			            		})
			            	}
			            })
			        }
			    }
			});
		},
		liveOut(){
			if(this.orderList.length<=0){
				uni.showToast({
					icon:"none",
					title:"未入住"
				})
				return;
			}
			if(this.orderList[this.current].orderState<4){
				uni.showToast({
					icon:"none",
					title:"未接单，不能离店"
				})
				return;
			}
			uni.showModal({
			    title: '提示',
			    content: '是否确认离店',
			    success: (res)=>{
			        if (res.confirm) {
			            liveOutOrder({
			            	orderInfoId:this.orderList[this.current].id
			            }).then(res=>{
			            	if(res.code==200){
			            		uni.showToast({
			            			icon:"success",
			            			title:"离店成功"
			            		})
			            	}
							this.getOrder();
							this.current=0;
			            })
			        }
			    }
			});
			
		},
		callClock(){
			if(this.orderList.length<=0){
				uni.showToast({
					icon:"none",
					title:"未入住"
				})
				return;
			}
			if(this.orderList[this.current].orderState<4){
				uni.showToast({
					icon:"none",
					title:"未接单，不能使用该服务"
				})
				return;
			}
			this.show = true;
			this.isClock=true;
			this.params = {
				year: false,
				month: false,
				day: false,
				hour: true,
				minute: true,
				second: false
			}
		},
		continueToLive(){
			let reg = /^[+]{0,1}(\d+)$/;
			if(!reg.test(this.day)){
				uni.showToast({
					icon:"none",
					title:"输入错误"
				})
				return;
			}
			uni.showLoading({
				title:"请稍等",
				mask:true,
			})
			extensionOrder({
				orderInfoId:this.orderList[this.current].id,
				day:this.day
			}).then(res=>{
				if(res.code==200){
					let obj = res.data.prePayInfo;
					uni.requestPayment({
						provider:"wxpay",
						timeStamp:obj.timeStamp,
						nonceStr:obj.nonceStr,
						'package':obj.packageValue,
						paySign:obj.paySign,
						signType:obj.signType,
						success:(e)=>{
							if(e.errMsg=='requestPayment:ok'){
								extensionCheck({
									orderInfoId:res.data.orderInfo.id,
									outTradeNo:res.data.outTradeNo,
									day:this.day
								}).then(payRes=>{
									if(payRes.code==200){
										uni.hideLoading();
										this.modalName = null;
										uni.showToast({
											icon:"success",
											title:"续住成功"
										})
										this.getOrder();
										this.current=0;
									}
								})
							}
						},
						fail(err) {
							uni.hideLoading();
							uni.showToast({
								title:"取消支付",
								icon:"none",
								mask:true,
							})
							console.log(err);
						}
					})
				}
			})
		},
		callQt(){
			if(this.orderList.length<=0){
				uni.showToast({
					icon:"none",
					title:"未入住"
				})
				return;
			}
			this.openCall(this.orderList[this.current].hotel.hotLine)
		},
		extensionOrder(){
			if(this.orderList.length<=0){
				uni.showToast({
					icon:"none",
					title:"未入住"
				})
				return;
			}
			if(this.orderList[this.current].orderState<4){
				uni.showToast({
					icon:"none",
					title:"未接单，不能使用该服务"
				})
				return;
			}
			this.modalName = 'extensionOrder';
			// this.show = true;
			// this.isClock=false;
			// this.params={
			// 	year: false,
			// 	month: true,
			// 	day: true,
			// 	hour: false,
			// 	minute: false,
			// 	second: false
			// }
		},
		swiperChange(event){
			this.current= event.detail.current
		},
		openModal(e){
			this.modalName=e;
		},
		openMap(hotel){
			uni.openLocation({
				latitude:Number(hotel.latitude),
				longitude:Number(hotel.longitude),
			})
		},
		getOrder(){
			coolOrderList().then(res=>{
				if(res.code==200){
					this.orderList = res.data;
					shareOrderList().then(res=>{
						if(res.code==200){
							if(res.data){
								this.orderList = this.orderList.concat(res.data);
							}
						}
					})
				}
			})
		},
		goOrder(){
			uni.navigateTo({
				url:"/pages/mine/orderList/orderList"
			})
		},
		savePwd(){
			if(this.pwd==null){
				uni.showToast({
					title: '密码不能为空',
					icon:"none"
				});
				return;
			}
			if(this.pwd.length<4||this.pwd.length>8){
				uni.showToast({
					title: '密码只能是4-8位',
					icon:"none"
				})
				return;
			}
			if(!this.$u.test.digits(this.pwd)){
				uni.showToast({
					title: '密码只能是数字',
					icon:"none"
				})
				return;
			}
			// if(this.newPwd!==this.pwd){
			// 	uni.showToast({
			// 		title: '两次密码不相同',
			// 		icon:"none"
			// 	})
			// 	return;
			// }
			changePassword({
				password:this.pwd
			}).then(res=>{
				if(res.code==200){
					uni.showToast({
						title: '修改成功',
						icon:"success"
					})
					this.hideModal();
				}
			})
		},
		hideModal(){
			this.modalName=null
		},
		updatePwd(){
			
		},
		openCall(e){
			uni.makePhoneCall({
			    phoneNumber: e //仅为示例
			});
		}
	}
}